package com.prbqb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.prbqb.pojo.PageBo;
import com.prbqb.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/blog")
public class PageController {

    @Autowired
    BlogService blogService;

    @GetMapping("/index")
    public ModelAndView index(){

        ModelAndView mv = new ModelAndView();


        Page blogs = blogService.listBlog(PageBo.firstPage() , PageBo.defaultPageSize);
        System.out.println("blogs is :" + blogs);

        mv.setViewName("index");
        mv.addObject("blogs" , blogs);
        mv.addObject("currentPage" , PageBo.currentPage);
        PageBo.totalPage = Integer.parseInt(blogs.getPages() + "");

        mv.addObject("totalPage" , PageBo.totalPage);


        return mv;
    }

    @GetMapping("/first")
    public ModelAndView first(){

        ModelAndView mv = new ModelAndView();

        Page blogs = blogService.listBlog(PageBo.firstPage() , PageBo.defaultPageSize);

        mv.setViewName("index");
        mv.addObject("blogs" , blogs);
        mv.addObject("currentPage" , PageBo.currentPage);
        mv.addObject("totalPage" , PageBo.totalPage);

        return mv;
    }

    @GetMapping("/pre")
    public ModelAndView pre(){

        ModelAndView mv = new ModelAndView();

        Page blogs = blogService.listBlog(PageBo.prePage() , PageBo.defaultPageSize);

        mv.setViewName("index");
        mv.addObject("blogs" , blogs);
        mv.addObject("currentPage" , PageBo.currentPage);
        mv.addObject("totalPage" , PageBo.totalPage);

        return mv;
    }

    @GetMapping("/next")
    public ModelAndView next(){

        ModelAndView mv = new ModelAndView();

        Page blogs = blogService.listBlog(PageBo.nextPage() , PageBo.defaultPageSize);

        mv.setViewName("index");
        mv.addObject("blogs" , blogs);
        mv.addObject("currentPage" , PageBo.currentPage);
        mv.addObject("totalPage" , PageBo.totalPage);

        return mv;
    }

    @GetMapping("/last")
    public ModelAndView last(){

        ModelAndView mv = new ModelAndView();

        Page blogs = blogService.listBlog(PageBo.lastPage() , PageBo.defaultPageSize);

        mv.setViewName("index");
        mv.addObject("blogs" , blogs);
        mv.addObject("currentPage" , PageBo.currentPage);
        mv.addObject("totalPage" , PageBo.totalPage);

        return mv;
    }

}

